package com.neuedu.wemall.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.neuedu.wemall.dao.entity.Cart;
import com.neuedu.wemall.service.ICartService;
import com.neuedu.wemall.dao.mapper.CartMapper;

import java.util.List;

@Service("cartService")
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {
    @Override
    public int insertCart(Cart cart) {
        int prodId=cart.getProdId();
        int userId=cart.getUserId();
        //查询当前会员用户购物车中是否存在该商品
        LambdaQueryWrapper<Cart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Cart::getUserId,userId);
        lqw.eq(Cart::getProdId,prodId);
        Cart cartDB=getBaseMapper().selectOne(lqw);
        if(cartDB==null){
            cart.setQty(1);//购买数量默认1
            return getBaseMapper().insert(cart);
        }
        //如果存在，数量+1
        cartDB.setQty(cartDB.getQty()+1);
        return getBaseMapper().updateById(cartDB);
    }
    @Override
    public Page<Cart> selectMemberCartList(int pageNum, int pageSize, int userId) {
        Page<Cart> page=new Page<>(pageNum,pageSize);
        return getBaseMapper().selectMemberCartList(userId,page);
    }
}